/**
 * Created by Eric.Dou on 4/14/14.
 */
define(function (require) {
    var comm = require('lib/commons');

    function toLazyLoadComponent($, options, target) {
        var html = options['html'], lazyLoadSub = html['lazyLoad'], loadText = '<p>loading...</p>', ajaxUrl = '', lazyMethod = 'getJSON', requestObj, saveProcess;
        if (comm.hasPro(lazyLoadSub, 'lazyLoadContent')) {
            loadText = lazyLoadSub['lazyLoadContent']
        }
        //add the the lazy loading content into the component inner.
        $(target).html(loadText);
        //loading the content from server.
        //the method possible options: "getJson","get","load","post"
        //Required property
        ajaxUrl = lazyLoadSub['url'];
        if (typeof(ajaxUrl) == 'undefined') {
            console.error('the "url" property must be assigned.')
        }
        if (comm.hasPro(lazyLoadSub, 'method')) {
            lazyMethod = lazyLoadSub['method'];
        }
        if (comm.hasPro(lazyLoadSub, 'data')) {
            requestObj = lazyLoadSub['data'];
        }
        //  saveProcess = [addEventListenerfunction];
        handleFn = function (data, status, xhr) {
            //load content finished,then put the lazyLoad flag to false.
            options['lazyLoad'] = false;
            //if there are a handle process then to handle it by itself
            if (comm.hasPro(lazyLoadSub, 'handle') && $.isFunction(lazyLoadSub['handle'])) {
                var innerHtml = lazyLoadSub['handle']($, data, status);
                if ('success' == status) {
                    if (comm.hasPro(lazyLoadSub, 'afterSuccess') && $.isFunction(lazyLoadSub['afterSuccess'])) {
                        lazyLoadSub['afterSuccess']($, data, status);
                    } else {
                        $(target).html('');
                    }
                    comm.appendHtmlToComponent(target, {content: innerHtml});
                }
            } else {
                //if status code 200, using the default handing method.
                if ('success' == status) {
                    comm.appendHtmlToComponent(target, data);
                }
            }

            $.each(saveProcess, function (i, fn) {
                fn(options);
            });
        };
        if ($.isFunction($[lazyMethod])) {
            console.log(lazyMethod)
        }

        if (typeof(requestObj) == 'undefined') {
            $[lazyMethod](ajaxUrl, handleFn)
        } else {
            $[lazyMethod](ajaxUrl, requestObj, handleFn)
        }
    }

    return  function (helper, $) {
        helper.register('wrapWidgetHtml', function (options) {
            this.condition=function(opt)
            {
                if (!comm.hasPro(options, 'target')) {
                    return false;
                }
                if (!comm.hasPro(options, 'html')) {
                    return false;
                }
                var html = options['html'];
                //if have the lazyload property mean ,the content should be lazy load by server response.SO don't need do nothing.
                if (comm.hasPro(html, 'lazyLoad')) {
                    return false;
                }
                return true;
            }
            var selecorID = 'target', target = options[selecorID], el = '<#ele></#ele>';
            if ($(target).length == 0) {
                if (typeof target == 'string') {
                    if (target.indexOf('#') >= 0) {
                        selecorID = target.substr(target.indexOf('#') + 1);
                    }
                }
                if (!comm.hasPro(options, 'nodeElement')) {
                    el = el.replace('#ele', 'div');
                } else {
                    el = el.replace('#ele', options['nodeElement']);
                }
                if (!comm.hasPro(options, 'attr')) {
                    $.extend({id: selecorID}, options['attr']);
                }
                $(el).appendTo('body').attr({id: selecorID});
            }


            var html = options['html'];

            var toInnerHtml = "";

            if ($.isFunction(html)) {
                html($);
                return {'wrapWidgetHtml': true};
            }

            if (comm.hasPro(html, 'lazyLoad')) {
                toLazyLoadComponent($, options, target);
            } else {
                comm.appendHtmlToComponent(target, html);
            }
            return {'wrapWidgetHtml': true};
        });
    }
});
